<?php

namespace App\Http\Controllers\Admin;

use App\Models\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class TagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tags=Tag::select()->with(['child'=>function($query){
            $query->select()->get();
        }])->where(['pid'=>0])->get();
        return view('Admin.Tag.index',compact('tags'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $tags=Tag::where(['pid'=>0])->get();
        return view('Admin.Tag.create',compact('tags'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
           'pid'=>'required',
//           'name'=>'required|unique:tags|max:7' //"unique:users,email_address,$selfId,id,deleted_at,NULL",
//           'name'=>"required|max:7|unique:tags,name,$selfId,id,deleted_at,Null" //"unique:users,email_address,$selfId,id,deleted_at,NULL",
            'name'=>['required','max:7',Rule::unique('tags')->where(function ($query) {
                $query->where('deleted_at', null);
            })]
        ],[
            'pid.required'=>'请选择标签类别',
            'name.required'=>'请填写标签名称',
            'name.unique'=>'该标签已存在',
            'name.max'=>'标签名最大长度为7'
        ]);
        if(Tag::create($request->all())){
            die("<script>parent.location.href='".route('tag.index')."'</script>");
        }else{
            return redirect()->back()->withInput()->withErrors('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $model=Tag::findOrFail($id);
        if($model->pid==0){
            $tags=Tag::where(['pid'=>-1])->get();
        }else{
            $tags=Tag::where(['pid'=>0])->get();
        }

        return view('Admin.Tag.edit',compact(['tags','model']));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'pid'=>'required',
//            'name'=>'required|max:7|unique:tags,name,'.$id
            'name'=>['required','max:7',Rule::unique('tags')->where(function ($query) use ($id) {
                $query->where('deleted_at', null)->where('id', '<>', $id);
            })]
        ],[
            'pid.required'=>'请选择标签类别',
            'name.required'=>'请填写标签名称',
            'name.unique'=>'该标签已存在',
            'name.max'=>'标签名称最大长度为7'
        ]);
        $model=Tag::findOrFail($id);
        $model->pid=$request->pid;
        $model->name=$request->name;
        if($model->save()){
            die("<script>parent.location.href='".route('tag.index')."'</script>");
        }else{
            return redirect()->back()->withInput()->withErrors('添加失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $flag=Tag::where(['pid'=>$id])->first();
        if($flag){
           return response()->json(['status'=>201,'msg'=>'该标签分类下有子标签,不能直接删除']);
        }
        $tag=Tag::find($id);
        if(!$tag){
           return response()->json(['status'=>202,'msg'=>'该标签已经被删除,请刷新页面']);
        }
        if($tag->delete()){
            return response()->json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return response()->json(['status'=>203,'msg'=>'删除失败']);
        }
    }
}
